﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;


namespace Admin.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            string userName = txtusername.Text.ToLower();
            string passWord = txtpassword.Text;
            if (UserBO.CheckUserNameExist(userName))
            {
                var userBo = UserBO.GetUserBO(userName);
                if (userBo.Islock == false)
                {
                    if (userBo.RoleID == 1 || userBo.RoleID == 2)
                    {
                        if (userBo.UserName == userName && userBo.Password == UserBO.encryptData(passWord))
                        {
                            Session["UserNameAdmin"] = userBo.UserName;
                            Session["RoleAdmin"] = userBo.RoleID;
                            //checkSavePassWithCookie();
                            if (userBo.RoleID == 1)
                                Response.Redirect("CauHinh.aspx");
                            else
                                Response.Redirect("NewsHouses.aspx");
                        }
                        else
                        {
                            lblstatus.Text = "Tài khoản hoặc mật khẩu không đúng.";
                        }
                    }
                    else
                    {
                        lblstatus.Text = "Tài khoản hoặc mật khẩu không đúng.";
                    }
                }
                else
                {
                    lblstatus.Text = "Tài khoản này đã bị khoá.";
                }
            }
            else
            {
                lblstatus.Text = "Tài khoản hoặc mật khẩu không đúng.";
            }             
        }

        protected void btncancel_Click(object sender, EventArgs e)
        {
                //txtusername.Text = "";
                //txtpassword.Text = "";
            
        }

        private void checkSavePassWithCookie()
        {
            //if (chkLuumatkhau.Checked)
            //{
            //    //FormsAuthentication.SetAuthCookie(ApplicationContext.User.Identity.Name, true);
            //    var userName = new HttpCookie("userNameAdmin", txtusername.Text) { Expires = DateTime.Now.AddDays(30) };
            //    var passWord = new HttpCookie("passWordAdmin", txtpassword.Text) { Expires = DateTime.Now.AddDays(30) };
            //    Response.Cookies.Add(userName);
            //    Response.Cookies.Add(passWord);
            //}
            //else
            //{
            //    var userName = new HttpCookie("userNameAdmin", null) { Expires = DateTime.Now };
            //    var passWord = new HttpCookie("passWordAdmin", null) { Expires = DateTime.Now };
            //    Response.Cookies.Add(userName);
            //    Response.Cookies.Add(passWord);
            //}
        }
    }
}
